set maxvar 32000
set matsize 11000
set emptycells drop
pwd

*****SETUP FULL YEAR DATA********
use pik pik_key_parent year parent_agi parent_wage parent_have_1040 dob_month dob_day dob_year ///
     dob_year_mom dob_month_mom dob_day_mom dob_year_dad birth_order parent_child_home parent_child_away ///
     parent_fil_stat pob_fips male us_born first_year_1040 ///
     using ${raw_data}/irs1040_universe_parent_fullyr.dta ///
     if (inrange(year,1979,1982) |  inrange(year,1984,1987) |  inrange(year,1989,1992)) ///
      & (inrange(dob_year,1979,1982) |  inrange(dob_year,1984,1987) |  inrange(dob_year,1989,1992)) ///
      & dob_year<=first_year_1040, clear
do ${main}/dos/tax/build/build_support/assign_var.do
gen mom_exactage = (dob - mdy(dob_month_mom,dob_day_mom,dob_year_mom))/365.25

destring pik, replace
xtset pik year
do ${main}/dos/tax/build/build_support/pred_agi_setup.do
save ${clean_data}/parents_fullyr.dta, replace

*****SETUP MAIN DATA********
use pik pik_key_parent year parent_agi parent_wage parent_have_1040 dob_month dob_day dob_year ///
     dob_year_mom dob_year_dad  birth_order parent_child_home parent_child_away ///
     parent_fil_stat pob_fips male us_born first_year_1040 ///
     using ${raw_data}/irs1040_universe_parent.dta if year <= $max_parent_year & dob_year<=first_year_1040, clear //RENAME FILE NAME
/*egg: destring pik*/
destring pik, replace

tab year

sort pik year
xtset pik year

keep if dob_month==1 | dob_month==2 |dob_month==11 |dob_month==12
do ${main}/dos/tax/build/build_support/assign_var.do
keep if taxyr_cut>=1979

xtset pik year
do ${main}/dos/tax/build/build_support/pred_agi_setup.do


sort pik year
gen pred0_agiz = .

set trace on
forvalues t=1/2 {
	reg parent_agiz i.l`t'.parent_agiz_bin##i.parent_maxage if inrange(year, 1994, 1995) | inrange(year, 1998, $max_parent_year)
  preserve
    use  ${clean_data}/parents_fullyr.dta, clear
    xtset pik year
    predict predL`t'_agiz , xb
    gen predL`t'_agiz_unadj = predL`t'_agiz*(cpi_jan/cpi_2015)
    capture gen pred0_agiz = .
    replace pred0_agiz = predL`t'_agiz if year==1969+`t' | year==1974+`t' | year==1979+`t' | year==1984+`t' | year==1989+`t' | year==1994+`t'
    save ${clean_data}/parents_fullyr.dta, replace
  restore
}

set trace off

use  ${clean_data}/parents_fullyr.dta, clear
gen pred_agiz = pred0_agiz
replace pred_agiz = parent_agiz if year==1969 | year==1974 | year==1979 | year==1984 | year==1989 | inrange(year, 1994, 1995) | inrange(year, 1998, $max_parent_year)

gen miss_pred_agiz =  pred_agiz==.
tab miss_pred_agiz

gen pred0_agiz_unadj = pred0_agiz*(cpi_jan/cpi_2015)
gen pred_agiz_unadj = pred_agiz*(cpi_jan/cpi_2015)

forvalues t=1/2 {
	gen i_agimissL`t' = (l`t'.parent_agiz==0)
	gen maxageL`t' = l`t'.parent_maxage
}

gen parent_mafid = .
do ${main}/dos/tax/build/build_support/mstat.do
do ${main}/dos/tax/build/build_support/birth_order.do

set more off
keep if taxyr_cut == year
merge m:1 taxyr_cut using ${supp_data}/eitc_params_yr, keep(match master) keepusing(eitc_in* eitc_out* eitc_max* ) gen(_m_eitc)

do ${main}/dos/tax/build/build_support/eitc_elig_slim.do


gen dob_dow = dow(dob)
sort year
save ${clean_data}/parents_fullyr.dta, replace
